//
//  YWEquipmentDetailCell.m
//  maintain
//
//  Created by Kyo on 16/4/6.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWEquipmentDetailCell.h"
#import "UIColor+CustomColors.h"
#import "UIView+Extension.h"
#import "YWMacro.h"
#import "YWConst.h"
#import "YWMissionManager.h"
#import "UIImageView+WebCache.h"
#import "YWEquipmentTroubleRecord.h"
#import "YWCommonTool.h"
#import "YWInspectionEquipmentRecord.h"
#import "YWHandler.h"
#import "YWPatrolPicture.h"
#import "YWPatrolPointRecord.h"

@interface YWEquipmentDetailCell ()
@property (nonatomic, weak) UILabel* dateLabel;
@property (nonatomic, weak) UILabel* maintanceTypeLabel;
@property (nonatomic, weak) UIImageView *recordImageView;
@property (nonatomic, weak) UIView *bgView;
@property (nonatomic, weak) UILabel *nameLabel;
@property (nonatomic, weak) UILabel *noteLabel;

@end

@implementation YWEquipmentDetailCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *identifier = @"YWEquipmentDetailCellIdentifier";
    YWEquipmentDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[YWEquipmentDetailCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor customBackgroudColor];
    }
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setUpSubViews];
    }
    return self;
}
- (UILabel *)dateLabel{
    if (!_dateLabel) {
        UILabel *dateLabel = [[UILabel alloc]init];
        dateLabel.font = KSmallContentFont;
//        dateLabel.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:dateLabel];
        _dateLabel = dateLabel;
    }
    return _dateLabel;
}
-(UILabel *)maintanceTypeLabel{
    if (!_maintanceTypeLabel) {
        UILabel *maintanceTypeLabel = [[UILabel alloc]init];
        maintanceTypeLabel.font = kContentFont;
        maintanceTypeLabel.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:maintanceTypeLabel];
        _maintanceTypeLabel = maintanceTypeLabel;
    }
    return _maintanceTypeLabel;
}
- (UIImageView *)recordImageView{
    if (!_recordImageView) {
        UIImageView *imageV = [[UIImageView alloc]init];
        imageV.layer.borderWidth = 1;
        imageV.layer.borderColor = [UIColor whiteColor].CGColor;
        [self.contentView addSubview:imageV];
        _recordImageView = imageV;
    }
    return _recordImageView;
}
-(UIView *)bgView{
    if (!_bgView) {
        UIView *bgVIew = [[UIView alloc]init];
        bgVIew.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:bgVIew];
        _bgView = bgVIew;
    }
    return _bgView;
}
-(UILabel *)nameLabel{
    if (!_nameLabel) {
        UILabel *nameLabel = [[UILabel alloc]init];
        nameLabel.font = kContentFont;
        [self.bgView addSubview:nameLabel];
        _nameLabel = nameLabel;
    }
    return _nameLabel;
}
-(UILabel *)noteLabel{
    if (!_noteLabel) {
        UILabel *noteLabel = [[UILabel alloc]init];
        noteLabel.font = kContentFont;
        [self.bgView addSubview:noteLabel];
        _noteLabel = noteLabel;
    }
    return _noteLabel;
}

- (void)setUpSubViews{
    self.dateLabel.hidden = NO;
    self.maintanceTypeLabel.hidden = NO;
    self.recordImageView.hidden = NO;
    self.bgView.hidden = NO;
    self.nameLabel.hidden = NO;
    self.noteLabel.hidden = NO;
}
-(void)setMaintenanceRecord:(YWEquipmentMaintenanceRecord *)maintenanceRecord{
    _maintenanceRecord = maintenanceRecord;
    
    [self setupView:maintenanceRecord];
    
    if (maintenanceRecord.dayMonthString.length>=2) {
        NSMutableAttributedString *attriString = [[NSMutableAttributedString alloc]initWithString:maintenanceRecord.dayMonthString];
        NSRange range = NSMakeRange(0, 2);
        [attriString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:19] range:range];
        self.dateLabel.attributedText = attriString;
    }else{
        self.dateLabel.attributedText = nil;
    }
    
    NSString *typeName = [YWCommonTool maintenanceTypeOfMaintenanceTypeId:maintenanceRecord.maintenanceTypeId];
    self.maintanceTypeLabel.text = typeName;
    self.nameLabel.text = [NSString stringWithFormat:@"执行人: %@",maintenanceRecord.handlerName?:@"无"];
    self.noteLabel.text = [NSString stringWithFormat:@"备注: %@",maintenanceRecord.note.length ? maintenanceRecord.note : @"未填写"];
}

-(void)setInspectionEquipmentRecord:(YWInspectionEquipmentRecord *)inspectionEquipmentRecord{
    _inspectionEquipmentRecord = inspectionEquipmentRecord;
    [self setupView:inspectionEquipmentRecord];
    
    if (inspectionEquipmentRecord.dayMonthString.length>=2) {
        NSMutableAttributedString *attriString = [[NSMutableAttributedString alloc]initWithString:inspectionEquipmentRecord.dayMonthString];
        NSRange range = NSMakeRange(0, 2);
        [attriString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:19] range:range];
        self.dateLabel.attributedText = attriString;
    }else{
        self.dateLabel.attributedText = nil;
    }
    
//    NSString *typeName = [YWCommonTool maintenanceTypeOfMaintenanceTypeId:YWMaintenanceTypePatrol];
    self.maintanceTypeLabel.text = @"巡查";
    self.nameLabel.text = [NSString stringWithFormat:@"执行人: %@",inspectionEquipmentRecord.executor.userName?:@"无"];
    self.noteLabel.text = [NSString stringWithFormat:@"备注: %@",inspectionEquipmentRecord.note.length ? inspectionEquipmentRecord.note : @"未填写"];
}

- (void)setPatrolPointRecord:(YWPatrolPointRecord *)patrolPointRecord{
    _patrolPointRecord = patrolPointRecord;
    [self setupView:patrolPointRecord];
    if (patrolPointRecord.dayMonthString.length>=2) {
        NSMutableAttributedString *attriString = [[NSMutableAttributedString alloc]initWithString:patrolPointRecord.dayMonthString];
        NSRange range = NSMakeRange(0, 2);
        [attriString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:19] range:range];
        self.dateLabel.attributedText = attriString;
    }else{
        self.dateLabel.attributedText = nil;
    }
    
//    NSString *typeName = [YWCommonTool maintenanceTypeOfMaintenanceTypeId:YWMaintenanceTypeKeepWatch];
    self.maintanceTypeLabel.text = @"巡更";
    self.nameLabel.text = [NSString stringWithFormat:@"执行人: %@",patrolPointRecord.executor.userName?:@"无"];
    self.noteLabel.text = [NSString stringWithFormat:@"备注: %@",patrolPointRecord.note.length ? patrolPointRecord.note : @"未填写"];
}

- (void)setupView:(id)record{
    CGFloat leftW = 60;
    CGFloat imageSize = 54;
    CGFloat gap = 3;
    NSArray *pictures = nil;
    if ([record isKindOfClass:[YWInspectionEquipmentRecord class]]) {
        YWInspectionEquipmentRecord *equipmentRecord = (YWInspectionEquipmentRecord*)record;
        pictures = equipmentRecord.pictures;
    }else if ([record isKindOfClass:[YWPatrolPointRecord class]]){
        YWPatrolPointRecord *pointRecord = (YWPatrolPointRecord*)record;
        pictures = pointRecord.pictures;
    }else{
        YWEquipmentMaintenanceRecord *maintanceRecord = (YWEquipmentMaintenanceRecord*)record;
        pictures = maintanceRecord.recordPictures;
    }
    [self.dateLabel setFrame:CGRectMake(kMarginW, gap, leftW, kLabelH)];
    [self.maintanceTypeLabel setFrame:CGRectMake(kMarginW, CGRectGetMaxY(self.dateLabel.frame)+gap, leftW, kLabelH)];
    NSString *smallPictureUrl = @"";
    if (pictures.count>0) {
        if ([pictures[0] isKindOfClass:[YWPicture class]]) {
            YWPicture *picture = pictures[0];
            smallPictureUrl = picture.smallPictureUrl;
        }else if ([pictures[0] isKindOfClass:[YWPatrolPicture class]]){
            YWPatrolPicture *patrolPicture = pictures[0];
            smallPictureUrl = patrolPicture.smallUrl;
        }
        NSString* urlStr = [NSString stringWithFormat:@"%@%@", kAliyunImageUrl, smallPictureUrl];
        [self.recordImageView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"image_placeholder"]];
        self.recordImageView.hidden = NO;
        [self.recordImageView setFrame:CGRectMake(CGRectGetMaxX(self.dateLabel.frame)+kMarginW, 0, imageSize, imageSize)];
        [self.bgView setFrame:CGRectMake(CGRectGetMaxX(self.recordImageView.frame), 0, SCREEN_WIDTH-CGRectGetMaxX(self.recordImageView.frame), kLabelH*2+10)];
    }else{
        self.recordImageView.hidden = YES;
        [self.bgView setFrame:CGRectMake(CGRectGetMaxX(self.dateLabel.frame)+kMarginW, 0, SCREEN_WIDTH-CGRectGetMaxX(self.dateLabel.frame)-kMarginW, kLabelH*2+10)];
    }
    
    [self.nameLabel setFrame:CGRectMake(kMarginW, gap, self.bgView.width-kMarginW*2, kLabelH)];
    [self.noteLabel setFrame:CGRectMake(kMarginW, CGRectGetMaxY(self.nameLabel.frame)+gap, self.nameLabel.width, kLabelH)];
}


@end
